import {
	refreshTokenRequest
} from '@/utils/auth'; // 刷新 Token 请求方法
import {
	baseUrl
} from '@/utils/baseUrl.js'; 
const BASE_URL = baseUrl;
let refreshTokenNum = 0

function refreshToken() {
	refreshTokenNum++
	return refreshTokenRequest()
}

export function request(options) {
	return new Promise((resolve, reject) => {
		
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: options.header ? Object.assign(options.header, {
				"Authorization": 'Bearer ' + uni.getStorageSync("token")
			}) : {
				"Authorization": 'Bearer ' + uni.getStorageSync("token")
			},
			success: (res) => {
				if (res.statusCode === 200) {
					if (res.data.code === 333) {
						refreshToken().then((rey) => {
					
								if (rey == 'success') {
									request(options).then((ret) => {
										
										resolve(ret)
									
									})
								} else {
									return uni.showToast({
										title: "授权失败，稍后再试！"
									})
								}
							})
							.catch((error) => {
								reject(error);
							});
					} else if (res.data.code === 403) {
						if (refreshTokenNum < 1) {
							refreshToken().then((rey) => {
								if (rey == 'success') {
									request(options).then((ret) => {
										refreshTokenNum = 0
									
										if (ret.code ==403) {
											uni.showToast({
												title: "您没有操作权限！"
											})
											
										}
										resolve(ret)

									})
								}
							})
						} else {
							resolve(res.data)
						}



					}else if(res.data.code === 300){
						
						uni.showToast({ title: res.data.msg, icon: "error" })
					}else if(res.data.code === 332){
						uni.removeStorageSync("token")
						return uni.showToast({
							title: "请重进入小程序",
							icon:"error"

						})
					} else {

						resolve(res.data)
					
					}
				} else {
					uni.showToast({
						title: "网络异常，请稍后再试！",
						 icon: "error"
					})
				
					reject(new Error(`请求失败，状态码：${res.statusCode}`));
				}
			},
			fail: (err) => {
				reject(err);
				uni.showToast({
					title: "网络异常，请稍后再试！"
				})
			},

			complete: (res) => {


			},
		});
	});
}
export function uploadFile(options) {
  return new Promise((resolve, reject) => {
          uni.uploadFile({
          			// url: baseUrl + '/api/upload/image', //仅为示例，非真实的接口地址
          			url: BASE_URL + options.url, //仅为示例，非真实的接口地址
                header: options.header ? Object.assign(options.header, {
                	"gangantao": uni.getStorageSync("token")
                }) : {
                	"gangantao": uni.getStorageSync("token")
                },
                //e.detail.avatarUrl,
          			filePath: options.data.avatar,
          			name: 'file',
          			// formData: {
          			// 	id:userInfo.value.id,
          			// },
          			success: (res) => {
                  if(res.statusCode == 200) {
                    resolve(JSON.parse(res.data))
                  } else {
                    uni.showToast({
                      title: "网络异常，请稍后再试！"
                    })
                  }
                },
                fail: (err) => {
                  reject(err);
                  uni.showToast({
                    title: "网络异常，请稍后再试！"
                  })
                },
        })
  })
}